package cn.hsw.platform.controller;

import cn.hsw.platform.core.doc.annotation.AuthPermission;
import cn.hsw.platform.core.base.BaseServiceController;
import cn.hsw.platform.core.dto.PageDto;
import cn.hsw.platform.core.dto.ResultDto;
import cn.hsw.platform.core.enums.HttpEnum;
import cn.hsw.platform.core.util.*;
import cn.hsw.platform.dto.SelectDto;
import cn.hsw.platform.model.RoleModel;
import cn.hsw.platform.service.RoleService;
import cn.hsw.platform.vo.RolePermissionVO;
import cn.hsw.platform.vo.RoleVO;
import com.alibaba.fastjson.JSONObject;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理
 * @author 李子木
 * @date 2020-11-10
 */
@RestController
@RequestMapping("/role")
public class RoleController extends BaseServiceController<RoleService> {

    /**
     * 新增
     * @param roleVO 入参
     * @return  -
     */
    @PostMapping("/add")
    @AuthPermission("manage:role:add")
    public ResultDto<Object> add(@Validated RoleVO roleVO){


        RoleModel roleModel = BeanUtil.copyProperties(roleVO, RoleModel.class);

        service.add(roleModel);

        return ResultUtil.success();
    }

    /**
     * 删除
     * @param roleModel 删除的模型
     * @return  -
     */
    @PostMapping("/del/{pid}")
    @AuthPermission("manage:role:del")
    public ResultDto<Object> del(@PathVariable("pid")RoleModel roleModel){

        Validator.validateNotNull(roleModel,"不存在或已删除");

        //判断角色下是否有用户，如果有则禁止删除
        if(service.validateRoleHaveUser(roleModel.getPid())){
            return ResultUtil.error(HttpEnum.BAD_GATEWAY.getCode(),"角色下包含用户,请先是删除用户");
        }

        service.delete(roleModel.getPid());

        return ResultUtil.success();
    }

    /**
     * 修改
     * @param roleModel 数据库中模型
     * @param roleVO    要修改的内容
     * @return  -
     */
    @PostMapping("/edit/{pid}")
    @AuthPermission("manage:role:edit")
    public ResultDto<Object> edit(@PathVariable("pid")RoleModel roleModel,RoleVO roleVO){

        Validator.validateNotNull(roleModel,"不存在或已删除");

        service.edit(roleModel,roleVO);

        return ResultUtil.success();
    }

    /**
     * 查 分页列表
     * @return ResultDto<PageDto<RoleModel>
     */
    @PostMapping("/list")
    @AuthPermission("manage:role:list")
    public ResultDto<PageDto<RoleModel>> list(){

        Page<RoleModel> list = service.findAll(PageUtil.pageable());

        return ResultUtil.success(list.getContent(),list.getTotalElements());
    }

    @PostMapping("/select")
    @AuthPermission("manage:role:select")
    public ResultDto<List<SelectDto>> select(){
        List<RoleModel> list = service.findAll();

        List<SelectDto> select = BeanUtil.copyListProperties(list, SelectDto::new
                ,(roleModel, selectDto) -> selectDto.setValue(roleModel.getPid()));

        return ResultUtil.success(select);
    }

    /**
     * @param body 参数 {"role_pid":,"permission_pids":[,,,,]}
     * @return success
     */
    @PostMapping("/authorization")
    @AuthPermission("manage:role:authorization")
    public ResultDto<?> authorization(@RequestBody String body){
        Validator.validateNotEmpty(body, HttpEnum.BAD_REQUEST.getMsg());

        RolePermissionVO rolePermissionVO = JSONObject.parseObject(body, RolePermissionVO.class);

        service.authorization(rolePermissionVO);

        return ResultUtil.success();
    }

    /**
     * @param
     * @return success
     */
    @PostMapping("/auth/{pid}")
    @AuthPermission("manage:role:have:auth")
    public ResultDto<RolePermissionVO> roleHaveAuth(@PathVariable("pid")Long rolePid){
        Validator.validateNotEmpty(rolePid, HttpEnum.BAD_REQUEST.getMsg());



        return ResultUtil.success(service.auth(rolePid));
    }

    @PostMapping("/info/{pid}")
    @AuthPermission("manage:role:info")
    public ResultDto<Object> info(@PathVariable("pid")Long pid){

        Validator.validateNotNull(pid,"主键ID必填");

        RoleModel roleModel = service.findById(pid);

        return ResultUtil.success(roleModel);
    }
}
